//
//  IntoDocumentsManager.m
//  FileDemo
//
//  Created by 徐小龙 on 2019/6/19.
//  Copyright © 2019 徐小龙. All rights reserved.
//

#import "IntoDocumentsManager.h"
#import "DocumentModel.h"

@implementation IntoDocumentsManager

+(NSArray *)GetAllIntoDocuments{
    
    NSMutableArray *documentModelList = [NSMutableArray array];
    
    //   在Document目录下的创建DataBase文件夹
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *inboxDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Inbox"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL fileExists = [fileManager fileExistsAtPath:inboxDirectory];
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (fileExists) {
        
        NSArray *paths = [fileManager subpathsAtPath:inboxDirectory];
        
        [array addObjectsFromArray:paths];
        
    }
    
    for (NSString *path in array) {
        
        if ([path hasPrefix:@"."]) {
            continue;
        }
        NSString *filePath = [NSString stringWithFormat:@"%@/%@",inboxDirectory,path];
        NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
        
        DocumentModel *fileModel = [[DocumentModel alloc] init];
        fileModel.documentName = path;
        fileModel.documentPath = filePath;
        fileModel.documentFileUrl = fileUrl;
        fileModel.documentExtension = fileUrl.pathExtension;
        
        BOOL isDirectory = NO;
        [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
        if (isDirectory) {
            fileModel.documentIconName = @"public_folder";
        }
        else{
            fileModel.documentIconName = [NSString stringWithFormat:@"public_%@",fileUrl.pathExtension];
        }
        
        fileModel.isDirectory = isDirectory;
        
        [documentModelList addObject:fileModel];
        
    }
    
    return documentModelList;
}
@end
